﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using UniRlv.COL;
using UniRlv.Utility;
using UniRlv.Utility.Extend;

namespace UniRlv.UI.Main.WarerHourseIn
{
    public partial class FrmExceptionHandle : UniRlv.UI.Main.FrmBase
    {
        List<string> m_saCodeBuf = new List<string>();
        exception_proccess m_ep;

        public FrmExceptionHandle()
        {
            InitializeComponent();
        }

        private void FrmExceptionHandle_Load(object sender, EventArgs e)
        {

            lstExceptionType.Items.AddRange(Const.GetEnumStringArray<exception_proccess>().ToArray());
            lstExceptionType.SelectedIndex = 0;

            toolBtnCancel.Click += toolBtnCancel_Click;
            toolBtnOK.Click += toolBtnOK_Click;

            toolBtnRun.Visible = true;
            toolBtnRun.Click += toolBtnRun_Click;

            toolBtnStop.Visible = true;
            toolBtnStop.Click += toolBtnStop_Click;

            WorkSpace.GetInstance().TaskPauseEvent += TaskPauseEventHandler;
            WorkSpace.GetInstance().TaskResumeEvent += TaskResumeEventHandler;
            WorkSpace.GetInstance().CodeEditEvent += CodeEditEventHandler;

            if (PublicObject.CurTask.state == (int)UniRlv.COL.task_state.TS_RUN)
            {
                toolBtnStop.Enabled = true;
                toolBtnRun.Enabled = false;
            }
            else
            {
                toolBtnStop.Enabled = false;
                toolBtnRun.Enabled = true;
            }

            panelDataSource.Size = panelDeleteRange.Size;
            panelDataSource.Location = panelDeleteRange.Location;

            panelPrint.Size = panelDeleteRange.Size;
            panelPrint.Location = panelDeleteRange.Location;

            panelReprint.Size = panelDeleteRange.Size;
            panelReprint.Location = panelDeleteRange.Location;

        }

        private void CodeEditEventHandler(object sender, CodeEditEventArg e)
        {
            CallCtrlWithThreadSafety.SetText(txtTip, e.Tip, this);
        }

        private void TaskResumeEventHandler(object sender, EventArgs e)
        {
            toolBtnRun.Enabled = false;
            toolBtnStop.Enabled = true;
        }

        private void TaskPauseEventHandler(object sender, EventArgs e)
        {
            toolBtnRun.Enabled = true;
            toolBtnStop.Enabled = false;
        }

        private void toolBtnStop_Click(object sender, EventArgs e)
        {
            WorkSpace.GetInstance().PauseTask();
        }

        private void toolBtnRun_Click(object sender, EventArgs e)
        {
            WorkSpace.GetInstance().ResumeTask();
        }

        void toolBtnOK_Click(object sender, EventArgs e)
        {
            string strMsg = string.Empty;
	        bool iRes = true;

            CallCtrlWithThreadSafety.SetText(txtTip, "", this);

            switch (m_ep)
	        {
	            case exception_proccess.EP_DELETE_REMAIN:
                    iRes = CodePrcUtility.SetRemainDelete(ref strMsg); 
                    break;
	            case exception_proccess.EP_UNBOX_REMAIN:
                    iRes = CodePrcUtility.SetRemainPackageState(package_state.PS_UNBOX, "", ref strMsg);
                    break;
	            case exception_proccess.EP_RESTART_TASK:
		            iRes = true;
                    WorkSpace.GetInstance().ReStartTask();
		            break;
	            case exception_proccess.EP_FINISH_TASK://结束任务
                    
		            if(PublicObject.CurTask.state==(int)task_state.TS_RUN)
		            {
			            ShowInfoMessageBox("提示", "先暂停生产任务！");
			            return;
		            }
		            else
			        {
                        WorkSpace.GetInstance().FinishTask(task_state.TS_STOP ,ref strMsg);
                        ShowInfoMessageBox("提示", "结束任务！");
                        FrmMain.GetInstance().SwitchFormByModuleName("TaskRun");
                    }
		            return;
	            case exception_proccess.EP_APPEND://附加一级尾数
		            if (m_saCodeBuf.Count==0)
		            {
			            ShowInfoMessageBox("提示", "请先扫描需要附加的一级码");
			            return;
		            }
                    //iRes = CodePrcUtility.AppendCode(m_saCodeBuf, 1, ref strMsg);
                    //break;
                    WorkSpace.GetInstance().PutEditData(m_saCodeBuf, m_ep);
                    OnReStart();
                    return;
                case exception_proccess.EP_LABEL_SWITCH://互换不常用，改为替换
		            if (m_saCodeBuf.Count<2 && !PublicObject.bPrint)
		            {
			            ShowInfoMessageBox("提示", "请扫描要替换的两个码");
			            return;
		            }
                    //iRes = CodePrcUtility.ReplaceCode(m_saCodeBuf, ref strMsg);
                    //break;
                    WorkSpace.GetInstance().PutEditData(m_saCodeBuf, m_ep);
                    OnReStart();
                    return;
                case exception_proccess.EP_LABEL_BATCH_DELETE:
                    if (m_saCodeBuf.Count==0)
		            {
			            ShowInfoMessageBox("提示", "请先扫描需要删除的码");
			            return;
		            }
                    //iRes = CodePrcUtility.DeleteInBatch(m_saCodeBuf, ref strMsg);
                    //break;
                    WorkSpace.GetInstance().PutEditData(m_saCodeBuf, m_ep);
                    OnReStart();
                    return;
                case exception_proccess.EP_LABEL_PACKAGE_DELETE:
		            if (m_saCodeBuf.Count==0)
		            {
			            ShowInfoMessageBox("提示", "请先扫描需要删除的码");
			            return;
		            }
                    //iRes = CodePrcUtility.DeletePackage(m_saCodeBuf[0], ref strMsg);
                    //break;
                    WorkSpace.GetInstance().PutEditData(m_saCodeBuf, m_ep);
                    OnReStart();
                    return;
                case exception_proccess.EP_LABEL_REPRINT://标签重打印，不影响码数量
                    if (m_saCodeBuf.Count == 0)
                    {
                        ShowInfoMessageBox("提示", "请先扫描需要重新打印的码");
                        return;
                    }

                    string codeThis = m_saCodeBuf[m_saCodeBuf.Count-1];
                    string codePre = m_saCodeBuf.IndexOf(codeThis) == 0 ? string.Empty : m_saCodeBuf[0];
                    string codeAfter = string.Empty;

                    if (m_saCodeBuf.Count>3)
                    {
                        codeAfter = m_saCodeBuf[2];
                    }
                    else if(m_saCodeBuf.Count == 3)
                    {
                        codeAfter = m_saCodeBuf.IndexOf(codeThis) == 0 ? m_saCodeBuf[1] : string.Empty;
                    }

                    if (rbAfter.Checked)
                    {
                        if (string.IsNullOrEmpty(codeAfter))
                        {
                            ShowInfoMessageBox("提示", "无后码，无法打印！");
                            return;
                        }

                        iRes = WorkSpace.GetInstance().RePrintOneLabel(codeAfter, (int)numRePrintQty.Value, ref strMsg);

                        if (!iRes)
                        {
                           // ShowErrorMessageBox("出现错误", "后码"+codeAfter + "打印失败：" + strMsg);
                            strMsg = "后码" + codeAfter + "打印失败：" + strMsg;
                        }
                    }

                    if (rbPre.Checked)
                    {
                        if (string.IsNullOrEmpty(codePre))
                        {
                            ShowInfoMessageBox("提示", "无前码，无法打印！");
                            return;
                        }

                        iRes = WorkSpace.GetInstance().RePrintOneLabel(codePre, (int)numRePrintQty.Value, ref strMsg);

                        if (!iRes)
                        {
                            //ShowErrorMessageBox("出现错误", "前码"+codePre + "打印失败：" + strMsg);
                            strMsg = "前码" + codePre + "打印失败：" + strMsg;
                        }
                    }

                    if (rbThis.Checked)
                    {

                        iRes = WorkSpace.GetInstance().RePrintOneLabel(codeThis, (int)numRePrintQty.Value, ref strMsg);

                        if (!iRes)
                        {
                            //ShowErrorMessageBox("出现错误", "码自身"+codeThis + "打印失败：" + strMsg);
                            strMsg = "码自身" + codeThis + "打印失败：" + strMsg;
                        }
                    }

                    if (rbParent.Checked)
                    {
                        CodeInfo ci = CodePrcUtility.GetParent(codeThis, ref strMsg);

                        if (ci == null)
                        {
                            ShowInfoMessageBox("提示", "父码不存在！");
                            return;
                        }
                        else
                        {
                            iRes = WorkSpace.GetInstance().RePrintOneLabel(ci.code, (int)numRePrintQty.Value, ref strMsg);

                            if (!iRes)
                            {
                                //ShowErrorMessageBox("出现错误", "码自身"+codeThis + "打印失败：" + strMsg);
                                strMsg = "父码" + ci.code + "打印失败：" + strMsg;
                            }
                        }
                    }
                    break;
                case exception_proccess.EP_ZERO_REMAIN:
		            if (m_saCodeBuf.Count==0 && !PublicObject.bPrint)
		            {
			            ShowInfoMessageBox("提示", "请先扫描外包装码");
			            return;
		            }
                    iRes = CodePrcUtility.SetRemainPackageState(package_state.PS_ZERO, m_saCodeBuf[0], ref strMsg);
		            break;
	            case exception_proccess.EP_LABEL_PRINT://标签打印，会影响码数量
                    int sta = lstStationPrint.SelectedIndex + 1;

                    iRes &= WorkSpace.GetInstance().PrintLabels(sta, (int)numPrintQty.Value, ref strMsg);
                    WorkSpace.GetInstance().RefreshCodeQty();

                    break;

                case exception_proccess.EP_MERGE_REMAIN:
                    m_saCodeBuf.Clear();
                    ShowInfoMessageBox("提示", "暂时不支持该操作");
                    return;
	            case exception_proccess.EP_INSERT://改为插入码
		            if (m_saCodeBuf.Count<2)
		            {
                        ShowInfoMessageBox("提示", "请扫描要操作的两个码：旧码和插入码");
			            return;
		            }
                    //iRes = CodePrcUtility.InsertCode(m_saCodeBuf, ref strMsg);
                    //break;
                    WorkSpace.GetInstance().PutEditData(m_saCodeBuf, m_ep);
                    OnReStart();
                    return;
                case exception_proccess.EP_QUERY_RLT:
                    m_saCodeBuf.Clear();
                    ShowInfoMessageBox("提示", "请到其他界面操作！");
		            return;
	            case exception_proccess.EP_SCAN_AUTO:
		            if (m_saCodeBuf.Count<2)
		            {
                        ShowInfoMessageBox("提示", "请扫描要操作的父子码");
			            return;
		            }
                    //iRes = CodePrcUtility.InsertCode21(m_saCodeBuf, ref strMsg);
                    //break;
                    WorkSpace.GetInstance().PutEditData(m_saCodeBuf, m_ep);
                    OnReStart();
                    return;
                case exception_proccess.EP_DELETE_RANGE:

                    if (numFrom.Value > numTo.Value)
                    {
                        ShowInfoMessageBox("提示", "结束值不能小于起始值");
                        return;
                    }

                    int st = Convert.ToInt32(numFrom.Value);
                    int ed = Convert.ToInt32(numTo.Value);
                    int station = lstStation.SelectedIndex + 1;

                    if (PublicObject.CurTask.tagLevel < station)
                    {
                        ShowInfoMessageBox("提示", "要删除的码级别不能超过本产品的包装级别");
                        return;
                    }
                    
                    if (MessageBox.Show(string.Format("确定删除从{0}到{1}的{2}级码？", st, ed, station), "请确认", MessageBoxButtons.OKCancel, 
                        MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        //iRes = CodePrcUtility.DeleteRange(station, st-1, ed-1, ref strMsg);
                        //break;
                        m_saCodeBuf.Clear();
                        m_saCodeBuf.Add(station.ToString());
                        m_saCodeBuf.Add((st - 1).ToString());
                        m_saCodeBuf.Add((ed - 1).ToString());
                        WorkSpace.GetInstance().PutEditData(m_saCodeBuf, m_ep);
                        OnReStart();
                        return;
                    }
                    else
                    {
                        return;
                    }
                case exception_proccess.EP_IMPORT_DATASOURCE:
                    if (string.IsNullOrEmpty(txtDataSourcePath.Text) || !File.Exists(txtDataSourcePath.Text))
                    {
                        ShowInfoMessageBox("提示", "数据源文件不存在，请先选择需要导入的数据源文件");
                        return;
                    }

                    iRes = WorkSpace.GetInstance().ImportDataSource(txtDataSourcePath.Text, lstDataSourceStation.SelectedIndex+1, ref strMsg);
                    break;
                default:
                    ShowInfoMessageBox("提示", "先选择需要处理的异常，或到相关页面处理操作！");
		            return;
	        }

	        OnReStart();

            if (iRes)
            {
                WorkSpace.GetInstance().RefreshCodeQty();

                if (m_ep != exception_proccess.EP_LABEL_PRINT && m_ep != exception_proccess.EP_LABEL_REPRINT)
                {//打印不需要提示
                    //ShowInfoMessageBox("提示", "操作成功");
                    CallCtrlWithThreadSafety.SetText(txtTip, "操作成功", this);
                }
                
            }
            else
            {
                if (m_ep != exception_proccess.EP_LABEL_PRINT && m_ep != exception_proccess.EP_LABEL_REPRINT)
                {//打印不需要提示
                    //ShowErrorMessageBox("操作失败", strMsg);
                    CallCtrlWithThreadSafety.SetText(txtTip, "操作失败：" + strMsg, this);
                }
                
            }
        }

        void toolBtnCancel_Click(object sender, EventArgs e)
        {
            OnReStart();
        }

        void OnReStart()
        {
            m_saCodeBuf.Clear();
            lstCodes.Items.Clear();
            toolBtnOK.Visible = false;
            toolBtnCancel.Visible = false;
        }

        private void txtCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (txtCode.Focused && e.KeyCode == Keys.Enter)
            {
                string s = AppHelper.SYZSGet24BitFromCodeNoCheck(txtCode.Text);

                if (string.IsNullOrEmpty(s))
                {
                    ShowWarnningMessageBox("警告", "所输入内容不是合法追溯码：" + txtCode.Text);
                    txtCode.Focus();
                    txtCode.SelectAll();
                }   
                else
                    AddCode(s);
            }
        }

        public override void OnScanCode(string orgCode, string validCode)
        {
            if(!AppHelper.SYZSCheckProduct(orgCode, 1))
            {
                ShowErrorMessageBox("出现错误", string.Format("产品或者文号不一致：当前产品={0}\r\n当前文号={1}\r\n当前扫描={2}", 
                    PublicObject.CurTask.tym, PublicObject.CurTask.pzwh, orgCode));

                return;
            }

            AddCode(validCode);
        }

        void AddCode(string code)
        {
            string strTip = string.Empty;

            if (m_saCodeBuf.Contains(code))
	        {
                ShowErrorMessageBox("出现错误", "不能重复输入条码");
		        return;
	        }

	        switch(m_ep)
	        {    
	        case exception_proccess.EP_LABEL_SWITCH:
		        if(m_saCodeBuf.Count==0)
		        {
			        strTip = string.Format("旧码：{0}", code);
		        }
		        else if (m_saCodeBuf.Count==1)
		        {
			        strTip = string.Format("新码：{0}", code);
                    toolBtnOK.Visible = true;
		        } 
		        else
		        {
			        ShowInfoMessageBox("提示", "只需扫描旧码和新码，请到工具栏执行当前操作");
			        return;
		        }

		        break;
	        case exception_proccess.EP_LABEL_BATCH_DELETE:
		        strTip = string.Format("顺序删除{1}：{0}", m_saCodeBuf.Count+1, code);break;
	        case exception_proccess.EP_LABEL_PACKAGE_DELETE:
		        strTip = string.Format("整包装删除{1}：{0}", m_saCodeBuf.Count+1, code);break;
	        case exception_proccess.EP_LABEL_REPRINT:
		        //strTip = string.Format("标签重打{1}：{0}", m_saCodeBuf.Count+1, code);break;

                    CodeInfo ci = CodePrcUtility.FindCode(code);
                    if (ci == null)
                    {
                        ShowInfoMessageBox("提示", "码未入库，无法重打印");
                        return;
                    }
                    else
                    {
                        int idx = CodePrcUtility.FindCodePos(ci);
                        List<CodeInfo> codes = CodePrcUtility.FindArrayByPos1(ci.stationNumber, idx - 1, 3);

                        strTip = "标签重打印：\r\n";
                        for (int i = 0; i < codes.Count; i++)
                        {
                            m_saCodeBuf.Add(codes[i].code);

                            if (codes[i].code.Equals(ci.code))
                            {
                                strTip += "码自身：" + codes[i].code + "  ";
                            }
                            else
                            {
                                if (i == 0)
                                {
                                    strTip += "前码：" + codes[i].code + "  ";
                                }
                                else
                                {
                                    strTip += "后码：" + codes[i].code + "  ";
                                }
                            }
                        }
                    }
                    break;
	        case exception_proccess.EP_ZERO_REMAIN:
		        if(PublicObject.bPrint)
		        {
			        strTip = string.Format("外包装码自动打印，请直接确认当前操作");
		        }
		        else
			        strTip = string.Format("外包装码【零箱】：{0}", code);
		        break;
	        case exception_proccess.EP_INSERT:
		        if(m_saCodeBuf.Count==0)
		        {
			        strTip = string.Format("旧码：{0}", code);
		        }
		        else if (m_saCodeBuf.Count==1)
		        {
			        strTip = string.Format("插入码：{0}", code);
                    toolBtnOK.Visible = true;
		        } 
		        else
		        {
			        ShowInfoMessageBox("提示", "只需扫描旧码和插入码，请到工具栏确认当前操作");
			        return;
		        }

		        break;

	        case exception_proccess.EP_SCAN_AUTO:
		        if(m_saCodeBuf.Count==0)
		        {
			        strTip = string.Format("二级码：{0}", code);
		        }
		        else
		        {
                    int raio21 = CodePrcUtility.GetStationRatio(2);
                    if (m_saCodeBuf.Count == (1 + raio21))
                    {
                        ShowInfoMessageBox("提示", "条码数量已满足一个二级包装，无须再扫描！");
                        return;
                    }
                    else
                    {
                        strTip = string.Format("一级码{0}：{1}", m_saCodeBuf.Count, code);
                        toolBtnOK.Visible = true;
                    }
			        
		        } 

		        break;
	        case exception_proccess.EP_APPEND:
		        strTip = string.Format("一级码{0}：{1}", m_saCodeBuf.Count+1, code);
                toolBtnOK.Visible = true;
		        break;
	        default:
		        return;
	        }

	        m_saCodeBuf.Add(code);
            toolBtnCancel.Visible = true;
            toolBtnOK.Visible = true;
            lstCodes.Items.Add(strTip);
        }

        private void lstExceptionType_SelectedIndexChanged(object sender, EventArgs e)
        {
	            int iSel = lstExceptionType.SelectedIndex;

	            if((int)m_ep != iSel && m_saCodeBuf.Count>0)
	            {
		            if (DialogResult.OK==MessageBox.Show("确认放弃当前操作，处理其他异常？", "请确认", MessageBoxButtons.OKCancel, 
                        MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
		            {
			            m_saCodeBuf.Clear();
		            } 
		            else
		            {
			            return;
		            }
	            }

	            lstCodes.Items.Clear();

	            m_ep = (exception_proccess)iSel;


            if (m_ep == exception_proccess.EP_DELETE_RANGE)
            {
                panelDeleteRange.Visible = true;
                panelPrint.Visible = false;
                lbCodeTip.Visible = false;
                txtCode.Visible = false;
                panelReprint.Visible = false;
                panelDataSource.Visible = false;
            }
            else if (m_ep == exception_proccess.EP_LABEL_PRINT)
            {
                panelPrint.Visible = true;
                panelDeleteRange.Visible = false;
                lstStationPrint.SelectedIndex = 0;
                lbCodeTip.Visible = false;
                txtCode.Visible = false;
                panelReprint.Visible = false;
                panelDataSource.Visible = false;
            }
            else if (m_ep == exception_proccess.EP_LABEL_REPRINT)
            {
                panelPrint.Visible = false;
                panelDeleteRange.Visible = false;
                lstStationPrint.SelectedIndex = 0;
                lbCodeTip.Visible = true;
                txtCode.Visible = true;
                panelReprint.Visible = true;
                panelDataSource.Visible = false;
            }
            else if (m_ep == exception_proccess.EP_IMPORT_DATASOURCE)
            {
                panelDataSource.Visible = true;
                lstDataSourceStation.SelectedIndex = 2;
                txtDataSourcePath.Text = "";

                panelDeleteRange.Visible = false;
                lbCodeTip.Visible = false;
                txtCode.Visible = false;
                panelReprint.Visible = false;
            }
            else
            {
                lstStation.SelectedIndex = 0;
                txtCode.Visible = true;
                panelPrint.Visible = false;
                panelDeleteRange.Visible = false;
                panelReprint.Visible = false;
                lbCodeTip.Visible = true;
                panelDataSource.Visible = false;
            }

                

                switch (m_ep)
	            {
	                case exception_proccess.EP_LABEL_SWITCH:
		                lstCodes.Items.Add("先扫旧码，再扫新码");
		                break;
	                case exception_proccess.EP_LABEL_BATCH_DELETE:
		                lstCodes.Items.Add("顺序扫描需要删除的码");break;
	                case exception_proccess.EP_LABEL_PACKAGE_DELETE:
		                lstCodes.Items.Add("扫描父码，连同子码一起删除");break;
	                case exception_proccess.EP_LABEL_REPRINT:
		                lstCodes.Items.Add("扫描需要重新打印的标签");break;
	                case exception_proccess.EP_UNBOX_REMAIN:
		                lstCodes.Items.Add("剩余未包装的产品设置为散装");
		                toolBtnOK.Visible = true;
		                break;
	                case exception_proccess.EP_ZERO_REMAIN:
		                if(PublicObject.bPrint)
		                {
			                lstCodes.Items.Add("剩余未包装的产品设置为零箱，请确认当前操作");
		                }
		                else
		                {
			                lstCodes.Items.Add("剩余未包装的产品设置为零箱，请扫描外层包装码");
		                }
		                break;
	                case exception_proccess.EP_MERGE_REMAIN:
		                lstCodes.Items.Add("自动拼箱(须先零箱)，后选择零箱和拼入的批次");
		                break;
	                case exception_proccess.EP_DELETE_REMAIN:
		                lstCodes.Items.Add("删除剩余未包装的产品");
		                toolBtnOK.Visible = true;
		                break;
	                case exception_proccess.EP_RESTART_TASK:
		                lstCodes.Items.Add("重新启动本次生产任务，清除本任务已生产信息");
		                toolBtnOK.Visible = true;
		                break;
	                case exception_proccess.EP_LABEL_PRINT:
		                lstCodes.Items.Add("打印标签，请选择标签的码级别");
		                toolBtnOK.Visible = true;
		                break;
	                case exception_proccess.EP_INSERT:
		                lstCodes.Items.Add("先扫旧码，再扫插入码");
		                break;
	                case exception_proccess.EP_SCAN_AUTO:
		                lstCodes.Items.Add("先扫二级码，再扫一级码");
		                break;
	                case exception_proccess.EP_APPEND:
		                lstCodes.Items.Add("顺序扫描需要附加的码");
		                break;
	                case exception_proccess.EP_FINISH_TASK:
		                lstCodes.Items.Add("结束本次生产任务，重新建立任务");
		                toolBtnOK.Visible = true;
		                break;
                    case exception_proccess.EP_DELETE_RANGE:
                        lstCodes.Items.Add("删除范围内条码，请慎重操作。注意，一级码和二级码按照二次采集位置来判断删除");
                        toolBtnOK.Visible = true;
                        break;
                    case exception_proccess.EP_IMPORT_DATASOURCE:
                        lstCodes.Items.Add("重新导入新的打印数据文件");
                        toolBtnOK.Visible = true;
                        break;
                default:
		                lstCodes.Items.Add("本页面不支持此项操作，请到其他页面执行相应操作");
		                toolBtnOK.Visible = false;
                        break;
	            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "(*.txt)文本文件|*.txt|(*.*)所有文件|*.*";
            ofd.Title = "请选择数据源文件";
            ofd.Multiselect = false;
            ofd.CheckFileExists = true;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                txtDataSourcePath.Text = ofd.FileName;
            }
        }

        private void lstExceptionType_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0)
            {
                return;
            }
            e.DrawBackground();
            e.DrawFocusRectangle();
            e.Graphics.DrawString(lstExceptionType.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);

        }
    }
}
